<?php
class susheControl extends skymvc{
	
	public function __construct(){
		parent::__construct();
	}
	
	public function onDefault(){
		
		
	}
	
	public function onMy(){
		$uka=M("youka")->get(M("login")->userid,2);
		$sushe=M("sushe")->selectRow("userid=".M("login")->userid);
		
		if(empty($sushe)){
			$s=M("sushe_user")->selectRow("userid=".M("login")->userid);
			if($s){
				$sushe=M("sushe")->selectRow("id=".$s['sushe']);
			}
			
		}
		if($sushe){
			$sscy=M("sushe_user")->select(array("where"=>"sushe=".$sushe['id']));
			if($sscy){
				foreach($sscy as $v){
					$uids[]=$v['userid'];
				}
				$us=M("user")->getUserByIds($uids);
				foreach($us as $k=>$v){
					$v['youka']=M("youka")->get($v['userid']);
					$v['jz_money']=$v['youka']['sxmoney']-$v['youka']['money'];
					$oids=M("koudai_order")->selectCols(array(
						"fields"=>" order_id ",
						"where"=>" userid=".$v['userid']." AND fenqi>0 AND isdz=0 AND status=3 "
					));
					if($oids){
						$v['jz_num']=M("koudai_order_product")->selectOne(array(
							"fields"=>" sum(amount) ",
							"where"=>" order_id in("._implode($oids).") "
						));
					}
					$us[$k]=$v;
				}
			}
		}else{
			$invitelist=M("sushe_invite")->select(array(
				"where"=>"status=0 AND userid=".M("login")->userid
			));
			if($invitelist){
				foreach($invitelist as $k=>$v){
					$fuser=M("user")->selectRow("userid=".$v['fuserid']);
					$v['sushe']=M("sushe")->selectRow("id=".$v['sushe']);
					
					$v['fuser']=$fuser;
				 
					$invitelist[$k]=$v;
				}
			}
		}
		 
		$this->smarty->assign(array(
			"sushe"=>$sushe,
			"sscy"=>$us,
			"invitelist"=>$invitelist
		));
		$this->smarty->display("sushe/my.html");
	}
	
	public function onAdd(){
		$id=get_post('id','i');
		if($id){
			$data=M("sushe")->selectRow("id=".$id);
			if($data && $data['sc_id']>0 ){
				$site_city=M("site_city")->selectRow("sc_id=".$data['sc_id']);
				$sclist2=M("site_city")->select(array(
						"where"=>" siteid=".SITEID." AND pid=".$site_city['pid']." ",
						"order"=>"orderindex ASC"
				));
			}
		}
		$this->smarty->assign(array(
			"data"=>$data,
			"sclist"=>M("site_city")->select(array(
				"where"=>" siteid=".SITEID." AND pid=0 ",
				"order"=>"orderindex ASC"
			)),
			"sclist2"=>$sclist2,
			"site_city"=>$site_city
		));
		$this->smarty->display("sushe/add.html");
	}
	
	public function onSave(){
		$data=M("sushe")->postData();
		$id=post('id');
		if(empty($data['title'])) $this->goAll("宿舍名称不能为空");
		if(empty($data['sc_id'])) $this->goAll("楼栋不能为空");
		$ck=M("sushe")->selectRow("sc_id=".$data['sc_id']." AND sushe=".$data['sushe']." ");
		if($id){
			$s=M("sushe_user")->selectRow("userid=".M("login")->userid);
			$sushe=M("sushe")->selectRow("id=".$id);
			
			if($ck && $ck['id']!=$sushe['id']){
				$this->goALl("该宿舍已经有人申请了");
			}
			if(empty($s)){
				$this->goAll("您无权限");
			}
			M("sushe")->update($data,"id=".$id);
		}else{
			if($ck){
				$this->goALl("该宿舍已经有人申请了");
			}
			$data['siteid']=SITEID;
			$data['userid']=M("login")->userid;
			$sushe=M("sushe")->insert($data);
			M("sushe_user")->insert(array(
				"userid"=>M("login")->userid,
				"sushe"=>$sushe,
				"siteid"=>SITEID,
				"dateline"=>time()
			));
			
		}
		$this->goALl("保存成功");
	}
	
	public function onInvite(){
		$this->smarty->display("sushe/invite.html");
	}
	
	public function onInvite_search(){
		$keyword=get_post('keyword','h');
		$userlist=M("user")->select(array("where"=>"telephone='".$keyword."' or nickname='".$keyword."'"));
		if($userlist){
			$this->goALl("success",0,$userlist);
		}else{
			$this->goAll("error",1);
		}
	}
	
	public function onInvite_save(){
		$userid=get('userid','i');
		$sushe=M("sushe_user")->selectRow("userid=".M("login")->userid);
		$user=M("user")->selectRow(" userid='".$userid."'");
		if($user){
			$s=M("sushe_user")->selectRow("userid=".$user['userid']);
			if($s){
				$this->goALl("该用户已加入宿舍",1);
			}else{
				M("sushe_invite")->insert(array(
					"userid"=>$user['userid'],
					"sushe"=>$sushe['sushe'],
					"fuserid"=>M("login")->userid,
					"siteid"=>SITEID,
					"dateline"=>time()
				));
			}
			$this->goALl("邀请加入成功，请等待用户确认");
		}
		$this->goALl("用户不存在",1);
	}
	
	public function onInvite_pass(){
		$id=get('id','i');
		$invite=M("sushe_invite")->selectRow("id=".$id." AND userid=".M("login")->userid." AND status=0 ");
		if($invite){
			M("sushe_user")->insert(array(
					"sushe"=>$invite['sushe'],
					"userid"=>M("login")->userid,
					"siteid"=>SITEID,
					"dateline"=>time()
			));
			M("sushe_invite")->update(array("status"=>1),"id=".$id);
			$this->goAll("加入成功");
		}else{
			$this->goAll("数据出错");
		}
		
	}
	
	
	public function onInvite_forbid(){
		$id=get('id','i');
		$invite=M("sushe_invite")->selectRow("id=".$id." AND userid=".M("login")->userid." AND status=0 ");
		if($invite){
			M("sushe_invite")->update(array("status"=>2),"id=".$id);
			$this->goAll("拒绝成功");
		}else{
			$this->goAll("数据出错");
		}
		
	}
	
	public function oNdelUser(){
		$orders=M("koudai_order_ssuser")->select(array(
			"where"=>" userid=".M("login")->userid." AND status=0 "
		));
		if($orders){
			$this->goAll("您还有未处理的宿舍订单，不能退出宿舍");
		}
		M("sushe_user")->delete("userid=".M("login")->userid);
		M("sushe")->update(array("userid"=>0),"userid=".M("login")->userid);;
		$this->goALl("退出成功");
	}
	
}

?>